import React, {PureComponent} from 'react';
import routes from './routes';
import PropTypes from 'prop-types';
import Export from '../common/Export';
import {PERMISSION} from './constants';
import {hasPermissions} from './utils';
import ImportPanel from './ImportPanel';
import {FORM_OPTIONS} from '../constants';
import TextArea from 'Shared/components/TextArea';
import TextInput from 'Shared/components/TextInput';
import {Button, Form, Row, Col, Card, message} from 'antd';
const ButtonGroup = Button.Group;
const FormItem = Form.Item;
class QueryPanel extends PureComponent {
    state = {
        showImportDelete: false
    }
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }
    handleFilterBatch = () => {
        this.props.querySearch();
        this.props.onClickBatchBtn();
    }
    // eslint-disable-next-line react/sort-comp
    onImportDelete = () => this.setState({showImportDelete: true});
    onCloseImportDelete = () => this.setState({showImportDelete: false});
    importDeleteChange = data => {
        if(data) {
            this.setState({showImportDelete: false});
            message.success(this.props.intl.formatMessage({
                id: 'queryPanel.msg.importDelete',
                defaultMessage: '导入删除成功'
            }));
        }
    }
    render() {
        const {formatMessage} = this.props.intl;
        const {conditions, permissions, querySearch,
            onResetClick, onClickExport, loading} = this.props;
        const {isDealerUser} = this.props.initEnumData;
        const operations = []; const otherBtns = [];
        if(hasPermissions(permissions, PERMISSION.importDelete))
            operations.push(
                <Button key="importDelete" onClick={this.onImportDelete}>
                    {formatMessage({
                        id: 'queryPanel.btn.importDelete',
                        defaultMessage: '导入删除'
                    })}
                </Button>
            );
        if(hasPermissions(permissions, PERMISSION.export))
            operations.push(
                <Export key={PERMISSION.export} exportRequest={onClickExport}/>
            );
        if(hasPermissions(permissions, PERMISSION.batchDelete))
            otherBtns.push(
                <Button key="batchDelete" onClick={this.handleFilterBatch}>
                    {formatMessage({
                        id: 'queryPanel.btn.batchDelete',
                        defaultMessage: '批量删除'
                    })}
                </Button>
            );
        return (
            <div>
                <Form className="form-standard">
                    <Card>
                        <Row>
                            {!isDealerUser && <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.dealerCode',
                                    defaultMessage: '总代号'
                                })}{...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="dealerCode"
                                        value={conditions.dealerCode}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onChange={this.handleFilterChange}/>
                                </FormItem>
                            </Col>}
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={formatMessage({
                                        id: 'queryPanel.form.partCodes',
                                        defaultMessage: '备件号'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <TextArea
                                        name="partCodes"
                                        placeholder={formatMessage({
                                            id: 'queryPanel.form.partCodes.placeholder',
                                            defaultMessage: '最多输入1000个，以回车隔开'
                                        })}
                                        value={conditions.partCodes}
                                        onBlur={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row>
                            <Col span={6}>
                                <Button key="query" type="primary" onClick={querySearch} loading={loading}>
                                    {formatMessage({
                                        id: 'queryPanel.btn.query',
                                        defaultMessage: '查询'
                                    })}</Button>
                                <Button key="clear" onClick={onResetClick}>
                                    {formatMessage({
                                        id: 'queryPanel.btn.reset',
                                        defaultMessage: '重置'
                                    })}</Button>
                            </Col>
                            <Col span={18} className="col-align-right">
                                <ButtonGroup>{otherBtns}{operations}</ButtonGroup>
                                {!isDealerUser && hasPermissions(permissions, PERMISSION.add) &&
                                    <Button key="add" type="primary" href={`#${routes.add.url()}`}>
                                        {formatMessage({
                                            id: 'queryPanel.btn.add',
                                            defaultMessage: '新增'
                                        })}
                                    </Button>}
                            </Col>
                        </Row>
                        {
                            this.state.showImportDelete &&
                                <ImportPanel
                                    selectKey={'2'}
                                    onCancel={this.onCloseImportDelete}
                                    onImportChange={this.importDeleteChange} />
                        }
                    </Card>
                </Form>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    initEnumData: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onClickBatchBtn: PropTypes.func.isRequired,
    onClickExport: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    intl: PropTypes.object,
};
import {connect} from 'react-redux';
import {querySearch, resetQueryPanel, saveQueryCondition, exportData, startBatchDel} from './actions';
import {createSelector} from 'reselect';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getinitEnumData = createSelector(
    state => state.getIn(['page', 'domainData', 'init']),
    init => {
        const initData = init.toJS();
        return {
            isDealerUser: initData.isDealerUser, // 是否为主机厂
            initData
        };
    }
);

import {injectIntl} from './intl';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const mapStateToProps = state => ({
    conditions: getConditions(state),
    initEnumData: getinitEnumData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    permissions: getPermissions(state),
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    querySearch: () => dispatch(querySearch()),
    onResetClick: () => dispatch(resetQueryPanel()),
    onClickExport: () => dispatch(exportData()),
    onClickBatchBtn: () => dispatch(startBatchDel()),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
